#include "Interface_Test.h"

#include "misc/Interface.h"
#include "Poco/Exception.h"
#include <iostream>
/*********************************************************/
std::string InterfaceTest::name()
{
	return("Interface");
}
/*********************************************************/
bool InterfaceTest::runUnitTests()
{
	bool result = true;

	if ( !cleanStartStopTest() ) result = false;
	if ( !singleInstanceTest() ) result = false;
	if ( !doubleStartStopTest() ) result = false;
	if ( !doubleStartTest() ) result = false;
	if ( !doubleStopTest() ) result = false;
	if ( !idleStopTest() ) result = false;

	return( result ) ;
}
/*********************************************************/
bool InterfaceTest::idleStopTest()
{
	try {
		Interface& e = Interface::Instance();		// Get the single instance of the class
		e.stop();					// Stop it
	
	} catch(Poco::Exception& e) {
		std::cout << "Running: idleStopTest()";
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false);
	} catch(...) {
		std::cout << "Running: idleStopTest()";
		std::cout << "... Unknown exception caught, trying to stop the Interface... Failed!" << std::endl;
		return( false );
	}
	
	// We had a clean destruction
	std::cout << "Running: idleStopTest()";
	std::cout << "... success!" << std::endl;
	return( true );
}
/*********************************************************/
bool InterfaceTest::doubleStartTest()
{
	try {
		Interface& e = Interface::Instance();		// Get the single instance of the class
		e.start("IN");					// Start the instance
		e.start("IN");					// Start the interface again
		e.stop();					// Stop it
	
	} catch(Poco::Exception& e) {
		std::cout << "Running: doubleStartTest()";
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false);
	} catch(...) {
		std::cout << "Running: doubleStartTest()";
		std::cout << "... Unknown exception caught, trying to stop the Interface... Failed!" << std::endl;
		return( false );
	}
	
	// We had a clean destruction
	std::cout << "Running: doubleStartTest()";
	std::cout << "... success!" << std::endl;
	return( true );
}
/*********************************************************/
bool InterfaceTest::doubleStopTest()
{
	try {
		Interface& e = Interface::Instance();		// Get the single instance of the class
		e.start("IN");					// Start the instance
		e.stop();					// Stop it
		e.stop();					// Stop it again
	
	} catch(Poco::Exception& e) {
		std::cout << "Running: doubleStopTest()";
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false);
	} catch(...) {
		std::cout << "Running: doubleStopTest()";
		std::cout << "... Unknown exception caught, trying to stop the Interface... Failed!" << std::endl;
		return( false );
	}
	
	// We had a clean destruction
	std::cout << "Running: doubleStopTest()";
	std::cout << "... success!" << std::endl;
	return( true );
}
/*********************************************************/
bool InterfaceTest::cleanStartStopTest()
{
	try {
		Interface& e = Interface::Instance();		// Get the single instance of the class
		e.start("IN");					// Start the instance
		e.stop();					// Stop the interface
	
	} catch(Poco::Exception& e) {
		std::cout << "Running: cleanStartStopTest()";
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false);
	} catch(...) {
		std::cout << "Running: cleanStartStopTest()";
		std::cout << "... Unknown exception caught, trying to stop the Interface... Failed!" << std::endl;
		return( false );
	}
	
	// We had a clean destruction
	std::cout << "Running: cleanStartStopTest()";
	std::cout << "... success!" << std::endl;
	return( true );
}
/*********************************************************/
bool InterfaceTest::doubleStartStopTest()
{
	try {
		Interface& e = Interface::Instance();		// Get the single instance of the class
		e.start("IN");					// Start the instance
		e.stop();					// Stop the interface
		e.start("IN");					// Start it again
		e.stop();					// Stop it again
	
	} catch(Poco::Exception& e) {
		std::cout << "Running: cleanStartStopTest()";
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false);
	} catch(...) {
		std::cout << "Running: cleanStartStopTest()";
		std::cout << "... Unknown exception caught, trying to stop the Interface... Failed!" << std::endl;
		return( false );
	}
	
	// We had a clean destruction
	std::cout << "Running: doubleStartStopTest()";
	std::cout << "... success!" << std::endl;
	return( true );
}
/*********************************************************/
bool InterfaceTest::singleInstanceTest()
{
	std::cout << "Running: singleInstanceTest()";
	Interface& e = Interface::Instance();
	Interface& ee = Interface::Instance();

	if ( &e == &ee ) {
		std::cout << "... success!" << std::endl;
		return( true );
	} else {
		std::cout << "... Failed!" << std::endl;
		return( false );
	}		
}
